<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="searchParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="事项主题" prop="title">
        <el-input v-model="searchParams.title" placeholder="请输入事项主题" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="事项状态" prop="status">
        <el-select v-model="searchParams.status" placeholder="请选择事项状态" clearable>
          <el-option v-for="(val, key) of itemStatusMap" :key="key" :value="key" :label="val">{{
              val
          }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="20" class="mb20">
      <el-col :span="6">
        <el-button type="primary" plain icon="el-icon-plus" size="small"
          @click="handleAdd">新建事项</el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="table.loading" :data="table.data">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="事项主题" prop="title" min-width="200" />
      <el-table-column label="发布日期" align="center" prop="startTime" width="160">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endTime" width="160">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事项状态" align="center" prop="status">
        <template v-slot="{ row }">
          <el-tag :type="getStatusType(row.status)" size="small" effect="plain">
            {{ itemStatusMap[row.status] || row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="{ row }">
          <el-button v-if="row.status == 1" type="text" size="small"
            @click="publish(row)">发布</el-button>
          <el-button v-if="row.status == 2" type="text" size="small"
            @click="cancel(row)">撤销</el-button>
          <el-button type="text" size="small" @click="view(row)">详情</el-button>
          <el-button type="text" size="small" @click="remove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="table.total > 0" :total="table.total" :page.sync="table.page.pageNo"
      :limit.sync="table.page.pageSize" @pagination="getList" />
  </div>
</template>

<script>
// import { DICT_TYPE, getDictDatas } from "@/utils/dict";
import { getEnterpriseList, deleteEnterpriseInfo, updateEnterpriseInfo } from '@/api/enterprise/info';

export default {
  data() {
    return {
      searchParams: {
        title: "",
        status: "",
      },
      // 事项状态选项
      itemStatusMap: {
        1: "待发布",
        2: "发布中",
        3: "已发布",
        // 4: "已删除",
      },
      table: {
        data: [],
        loading: false,
        total: 0,
        page: {
          pageNo: 1,
          pageSize: 10,
        },
      },
    };
  },
  created() {
    this.handleQuery();
  },
  methods: {
    handleQuery() {
      this.table.page.pageNo = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.table.loading = true;
      // 执行查询
      getEnterpriseList(this.searchParams)
        .then((res) => {
          const { list, total } = res?.data || {};
          this.table.data = list || [];
          this.table.total = total || 0;
        })
        .finally(() => {
          this.table.loading = false;
        });
    },
    handleAdd() {
      this.$router.push({ path: "/corporate/create" });
    },
    getStatusType(status) {
      return {
        1: "info",
        2: "warning",
        3: "success",
        // 4: "danger",
      }[status] || 'info';
    },
    publish({ id }) {
      this.$confirm("确认发布事项通知", "发布事项", {
        type: "info",
      })
        .then(() => {
          return updateEnterpriseInfo({ id, status: '2' });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("发布成功");
        });
    },
    cancel({ id }) {
      this.$confirm("目前未到设定的截止日期，是否提前结束？", "撤销事项", {
        type: "warning",
      })
        .then(() => {
          return updateEnterpriseInfo({ id, status: '1' });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("撤销成功");
        });
    },
    view({ id }) {
      this.$router.push({
        path: "/corporate/detail",
        query: { id },
      });
    },
    remove({ id }) {
      this.$confirm("该操作不可恢复，是否删除该事项？", "删除事项", {
        type: "warning",
      })
        .then(() => {
          return deleteEnterpriseInfo(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
    },
  },
};
</script>

<style>

</style>
